/* Name: StartScreen
 * Author: John Gideon
 * Date: 5-1-12
 * Description: Displays the start screen of the game. Lets user select a difficulty, room size,
 *  view help screen, and start game.
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class StartScreen extends Item {
	
	StartBtn startBtn;
	HelpBtn helpBtn;
	ExitBtn exitBtn;
	private static RadioBtn difficultyBtn[] = new RadioBtn[3];
	private static RadioBtn sizeBtn[] = new RadioBtn[3];
	private static int drawStep = 0;
	private static Boolean growing;
	private static int fadeIn1;
	private static int fadeIn2;
	
	public StartScreen() {
		Game.create((Item)this, 1); // Needed for update, draw, and destroy
		colType = COL_NONE; // Needed for collision checking		
		startBtn = new StartBtn(Game.screenWidth/2-90,Game.screenHeight-140,150,50);	
		helpBtn = new HelpBtn(Game.screenWidth/2+90,Game.screenHeight-140, 150, 50);		
		exitBtn = new ExitBtn(Game.screenWidth/2,Game.screenHeight-80, 150, 50);
		// Difficulty Buttons
		difficultyBtn[0] = new RadioBtn((Game.screenWidth/2)-350,Game.screenHeight-180, 170, 50);
		difficultyBtn[1] = new RadioBtn((Game.screenWidth/2)-350,Game.screenHeight-120, 170, 50);
		difficultyBtn[2] = new RadioBtn((Game.screenWidth/2)-350,Game.screenHeight-60, 170, 50);
		difficultyBtn[0].setText("EASY");
		difficultyBtn[1].setText("MEDIUM");
		difficultyBtn[2].setText("HARD");
		difficultyBtn[1].setSelected(true);
		difficultyBtn[0].addLink(difficultyBtn[1]); difficultyBtn[0].addLink(difficultyBtn[2]);
		difficultyBtn[1].addLink(difficultyBtn[0]); difficultyBtn[1].addLink(difficultyBtn[2]);
		difficultyBtn[2].addLink(difficultyBtn[0]); difficultyBtn[2].addLink(difficultyBtn[1]);
		// Size Buttons
		sizeBtn[0] = new RadioBtn((Game.screenWidth/2)+350,Game.screenHeight-180, 170, 50);
		sizeBtn[1] = new RadioBtn((Game.screenWidth/2)+350,Game.screenHeight-120, 170, 50);
		sizeBtn[2] = new RadioBtn((Game.screenWidth/2)+350,Game.screenHeight-60, 170, 50);
		sizeBtn[0].setText("SMALL");
		sizeBtn[1].setText("MEDIUM");
		sizeBtn[2].setText("LARGE");
		sizeBtn[1].setSelected(true);
		sizeBtn[0].addLink(sizeBtn[1]); sizeBtn[0].addLink(sizeBtn[2]);
		sizeBtn[1].addLink(sizeBtn[0]); sizeBtn[1].addLink(sizeBtn[2]);
		sizeBtn[2].addLink(sizeBtn[0]); sizeBtn[2].addLink(sizeBtn[1]);
		drawStep = 64;
		growing = true;
		fadeIn1 = 120;
		fadeIn2 = 240;
	}
	
	// Updates locations of buttons (in case screen size changes)
	public void update() {
		startBtn.updateLocation(Game.screenWidth/2-90,Game.screenHeight-140);
		helpBtn.updateLocation(Game.screenWidth/2+90,Game.screenHeight-140);
		exitBtn.updateLocation(Game.screenWidth/2,Game.screenHeight-80);
		// Difficulty Buttons
		difficultyBtn[0].updateLocation((Game.screenWidth/2)-350,Game.screenHeight-180);
		difficultyBtn[1].updateLocation((Game.screenWidth/2)-350,Game.screenHeight-120);
		difficultyBtn[2].updateLocation((Game.screenWidth/2)-350,Game.screenHeight-60);
		// Room Size Buttons
		sizeBtn[0].updateLocation((Game.screenWidth/2)+350,Game.screenHeight-180);
		sizeBtn[1].updateLocation((Game.screenWidth/2)+350,Game.screenHeight-120);
		sizeBtn[2].updateLocation((Game.screenWidth/2)+350,Game.screenHeight-60);
		if (growing) {
			if (++drawStep >= 255-64) {
				growing = false;
			}
		} else {
			if (--drawStep <= 0+64) {
				growing = true;
			}
		}
		// Set fading text
		if (fadeIn1 > 0) {--fadeIn1;};
		if (fadeIn2 > 0) {--fadeIn2;};
	}
	
	public void leftMousePress(){}
	public void rightMousePress(){}
	public void leftMouseRelease(){}
	public void rightMouseRelease(){}
	
	// Draw title screen - called whenever a new frame is drawn on screen
	public void draw() {
		Graphics2D gh = Game.getGraphicsHandle();
		// Draw Title
		drawTitle(gh);
		// Draw Radio Button Titles
		Font font = new Font("Serif", Font.PLAIN, 50);
		gh.setFont(font);
		gh.setColor(Color.GREEN);
		String text = "Difficulty";
		gh.drawChars(text.toCharArray(), 0, text.length(), (Game.screenWidth/2)-442, Game.screenHeight-225);
		text = "Room Size";
		gh.drawChars(text.toCharArray(), 0, text.length(), (Game.screenWidth/2)+240, Game.screenHeight-225);
	}
	
	// Draws the title on the screen
	private void drawTitle(Graphics2D gh) {
		// Set variables
		int titleX = Game.screenWidth/2;
		int titleY = 150;
		String text = "Space Conquerer";
		// Start drawing
		Font font = new Font("Sans_Serif", Font.HANGING_BASELINE, 120);
		
		gh.setColor(new Color(drawStep/(float)255, drawStep/(float)255, 1.0f, 1.0f-(fadeIn1/(float)120)));
		gh.setFont(font);
        
		// Figure out center location for text
        FontMetrics fontMetrics = gh.getFontMetrics();
        Rectangle stringBounds = fontMetrics.getStringBounds(text, gh).getBounds();
        
        // Draw text
		gh.drawChars(text.toCharArray(), 0, text.length(), (int)(titleX-stringBounds.width/2), (int)(titleY));
		
		// Figure out center location for text
		font = new Font("Serif", Font.PLAIN, 50);
		if (fadeIn2 <= 120) {
			gh.setColor(new Color(1.0f-(drawStep/(float)255), 1.0f-(drawStep/(float)255), 1.0f, 1.0f-((fadeIn2)/(float)120)));
			gh.setFont(font);
			text = "By Team Excelcior";
	        FontMetrics fontMetrics2 = gh.getFontMetrics();
	        Rectangle stringBounds2 = fontMetrics2.getStringBounds(text, gh).getBounds();
	        
	        // Draw text
			gh.drawChars(text.toCharArray(), 0, text.length(), (int)(titleX-stringBounds2.width/2), (int)(titleY+130));
		}
	}
	
	// Called when start game is pressed - sets everything up for a new game
	public static void setupNewRound() {
		String tmp = "New Round Started with ";
		if (difficultyBtn[0].selected) {Game.difficulty = Game.DIFFICULTY_EASY; tmp += "Easy";}
		else if (difficultyBtn[1].selected) {Game.difficulty = Game.DIFFICULTY_MEDIUM; tmp += "Medium";}
		else if (difficultyBtn[2].selected) {Game.difficulty = Game.DIFFICULTY_HARD; tmp += "Hard";}
		tmp += " Difficulty and ";
		if (sizeBtn[0].selected) {Game.roomSize = Game.SIZE_SMALL; tmp += "Small";}
		else if (sizeBtn[1].selected) {Game.roomSize = Game.SIZE_MEDIUM; tmp += "Medium";}
		else if (sizeBtn[2].selected) {Game.roomSize = Game.SIZE_LARGE; tmp += "Large";}
		tmp += " Room Size";
		Game.appendLog(tmp);
	}
	
	public void destroy() {}
}
